package com.cbnu.SmartCampusLife;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class SmartCampusLifeActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */

	String secUID = null;
	String secPWD = null;
	String uid = null;
	String pswd = null;
	int chk = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		WebView webview = (WebView) findViewById(R.id.main_webView);
		Button button = (Button) findViewById(R.id.main_Button);

		webview.getSettings().setUseWideViewPort(true);
		webview.getSettings().setLoadWithOverviewMode(true);
		webview.loadUrl("http://gaesin.chungbuk.ac.kr/login.html");

		button.setOnClickListener(this);

		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setBuiltInZoomControls(true);
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url); // URL load(update)
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				if (view.getUrl().equals("http://gaesin.chungbuk.ac.kr/jsp/login.jsp")) {
					Intent intent = new Intent(SmartCampusLifeActivity.this, BeforeMainMenu.class);
					startActivity(intent);
					finish();
				}
			}
		});
	}

	public void onClick(View v) {
		String path = "/data/data/com.cbnu.SmartCampusLife/files/";
		File stuIdChk = new File(path + "stu_id.txt");
		File stuNameChk = new File(path + "stu_name.txt");
		File stuPartChk = new File(path + "stu_part.txt");
		File stuStateChk = new File(path + "stu_state.txt");
		File timeMyChk = new File(path + "timeMy.txt");

		if (stuIdChk.isFile() && stuNameChk.isFile() && stuPartChk.isFile() && stuIdChk.isFile()
				&& stuStateChk.isFile() && timeMyChk.isFile()) {
			Intent intent = new Intent(SmartCampusLifeActivity.this, MainMenu.class);
			intent.putExtra("loginChk", 0);
			Toast.makeText(this, "최근 사용한 계정을 이용할 경우 종합정보시스템의 내용이 없거나 과거의 기록이 나올 수 있습니다.",
					Toast.LENGTH_LONG).show();
			startActivity(intent);
			finish();
		} else {
			Log.d("FileCheck", (stuIdChk.isFile() ? "true" : "false"));
			Log.d("FileCheck", (stuNameChk.isFile() ? "true" : "false"));
			Log.d("FileCheck", (stuPartChk.isFile() ? "true" : "false"));
			Log.d("FileCheck", (stuStateChk.isFile() ? "true" : "false"));
			Log.d("FileCheck", (timeMyChk.isFile() ? "true" : "false"));
			Toast.makeText(this, "로그인한 적이 없거나 과거 기록이 지워졌습니다. 로그인 해주세요.", Toast.LENGTH_LONG).show();
		}
	}
}